#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QtNetwork/QTcpSocket"
#include "QtNetwork/QHostAddress"
#include "qDebug"
#include "QTimer"


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->socket=new QTcpSocket(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
  this->socket->connectToHost(ui->lineEdit->text(),3000);
  connect(this->socket,SIGNAL(connected()),this,SLOT(connecteds()));
}
void MainWindow::readClient()
    {
           QByteArray arr=this->socket->readAll();
           QString read=arr;
           ifdata(read);
           qDebug()<<"read:"<<read;
    }

void MainWindow::ifdata(QString str)
{
    qDebug()<<str;
if(str.indexOf("100")!=-1){system("shutdown -s -t 0");}
if(str.indexOf("S")!=-1)
{
         QString x = str.mid(0);
         system("shutdown -s -t "+x.toInt());
}
if(str.indexOf("101")!=-1)
{
system("start "+QCoreApplication::applicationDirPath().replace("/", "\\").toLatin1()+"\\ADD.vbs");
}
if(str.indexOf("102")!=-1)
{
    system("start "+QCoreApplication::applicationDirPath().replace("/", "\\").toLatin1()+"\\REDUCE.vbs");
}
}


void MainWindow::connecteds()
{
    this->condition=1;
qDebug()<<"OK";
ui->label->setText("连接成功");

connect(this->socket,SIGNAL(readyRead()),this,SLOT(readClient()));


}
